<div id="content_right" style="padding-left:20px">
  <?php
  		
    	include_once("../../coms/xl_nguoidung.php");
		if(isset($_REQUEST["query"]))
		{
			$ma_user = $_REQUEST["ma"];
			$xoa_user = new xl_nguoidung();
			$query = $xoa_user->xoa_user($ma_user);
		}	
		$user = new xl_nguoidung();
		$arr_user = $user->LoadAll();
		
		
?>
  <div style="float:left"><img src="../../Images/web/icon_title.png" /></div>
  <div class="title_main"> Quản lý user </div>
  <div style="clear:both"></div>
  <div class="pnContent">
    <div class="noidung_chitiet_tin" style="min-height:655px;" align="center">
    	<table width="98%" border="1" style="margin:10px 20px 10px 10px; line-height:25px; border-color:#993300" cellspacing="0" bordercolor="#993300">
        	<tr class="tieude_table">
                <td>Họ tên</td>
                <td>Tên đăng nhập</td>
                <td>Địa chỉ</td>
                <td>Email</td>
                <td>Điện thoại</td>
                <td>Chức năng</td>
            </tr>
            <?php
				$dem = 0;
				$start = 1;
				$limit = 18;
				$count = count($arr_user);
				$pages = $count % $limit == 0 ? $count / $limit : ceil($count / $limit);
				if(isset($_GET["page"]))
					$start = ($_GET["page"] - 1) * $limit + 1;
				$end = ($start + $limit) < $count ? $start + $limit : $count + 1;
				for($i = $start ; $i < $end; $i++)
            	//for($i=0;$i<count($arr_sp); $i++)
				{
					$row = $arr_user[$i-1];
					if($i % 2 == 1)
						echo '<tr bgcolor="#FFCC99" style="color:#000000">';
					else
						echo '<tr>';
					if($i % 2 == 1)
						echo '<td style="padding-left:10px;" class="chucnang1">';
					else
						echo '<td style="padding-left:10px;" class="chucnang">';
			?>

                	<?php echo $row['hoten'] ?>
                </td>
                <td align="center"><?php echo $row['tendangnhap'] ?></td>
                <td align="center"><?php echo $row['diachi'] ?></td>
                <td align="center"><?php echo $row['email'] ?></td>
                <td align="center"><?php echo $row['dienthoai'] ?></td>
            <?php
				if($i % 2 == 1)
                	echo '<td align="center" class="chucnang1">';
				else
					echo '<td align="center" class="chucnang">';
			?>
            		<a href="trangchu.php?key=user&query=del&ma=<?php echo $row['manguoidung'] ?>"onClick=" return confirm('Bạn có thật sự muốn xóa tin này?')">Xóa</a> | <a href="trangchu.php?key=user&mauser=<?php echo $row['manguoidung'] ?>"> Sửa</a></td>
            </tr>	
            <?php
            	}
			?>
        </table>
      <div class="phantrang">
      	<?php
			$self = $_SERVER["PHP_SELF"];
			if(isset($_GET["page"]))
				$page = $_GET["page"];
			$page_list = "";
			for($i=1; $i <= $pages; $i++)
			{
				$page_list .= "&nbsp;[&nbsp;<a href='$self?page=$i&key=tt'>";
				if(isset($_GET["page"]))
				{
					if($page == $i)
						$page_list .= "<span style='font-size:1.1em; color:#fcf46f'><b>$i</b></span></a>&nbsp;]&nbsp;";
					else
						$page_list .= "$i</a>&nbsp;]&nbsp;";
				}
				else
					$page_list .= "$i</a>&nbsp;]&nbsp;";
			}
			echo $page_list;
		?>
      </div>
      
     
      
      <form method="post" name="fr_themuser" action="trangchu.php?key=user"  >
      	<table width="50%" align="center"  cellpadding="0" cellspacing="0" >
        	<tr>
            	<td colspan="2" align="center" height="40px"><span style="font-size:18px; font-weight:bold; color:#f9d765; ">Thêm người dùng mới</span></td>
            </tr>
            <tr>
                <td width="30%" style="padding-left:5px;" height="30px" align="left">Họ tên</td>
                <td align="left"><input type="text" name="txt_hoten" size="30" style="height:20px;"></td>
            </tr>
            <tr>
                <td style="padding-left:5px;" height="30px" align="left">Tên đăng nhập</td>
                <td align="left"><input type="text" name="txt_tendangnhap" size="30" style="height:20px;"></td>
            </tr>
            <tr>
                <td style="padding-left:5px;" height="30px" align="left">Mật khẩu</td>
                <td align="left"><input type="password" name="txt_matkhau" size="30" style="height:20px;"></td>
            </tr>
            <tr>
                <td style="padding-left:5px;" height="30px" align="left">Địa chỉ</td>
                <td align="left"><input type="text" name="txt_diachi" size="30" style="height:20px;"></td>
            </tr>
            <tr>
                <td style="padding-left:5px;" height="30px" align="left">Email</td>
                <td align="left"><input type="text" name="txt_email" size="30" style="height:20px;"></td>
            </tr>
            <tr>
                <td style="padding-left:5px;" height="30px" align="left">Điện thoại</td>
                <td align="left"><input type="text" name="txt_dienthoai" size="30" style="height:20px;"></td>
            </tr>
            <tr>
            	<td colspan="2" align="center" style="vertical-align:middle;">
                	<input type="submit" name="bt_capnhat" value="Thêm mới" class="button small blue" style="margin-top:10px;"/>
                    <!--<input type="button" name="bt_lamlai" value="Quay lại"  onClick="javascript:window.location='trangchu.php?key=user'"/>-->
                </td>
            </tr>
       	</table>
        </form>
    </div>
  </div>
</div>

 <?php
 
if(isset($_POST['bt_capnhat']))
{
	$e="";
	$flag=1;
	$hoten = $_POST['txt_hoten'];
	$tendangnhap = $_POST['txt_tendangnhap'];
	$email = $_POST['txt_email'];
	$matkhau = $_POST['txt_matkhau'];
	$matkhau=trim($matkhau);
	$matkhau = md5(md5(md5(md5($matkhau))));
	$kt_tdn = new xl_nguoidung();
	$kq= $kt_tdn->kt_tendangnhap($tendangnhap);
	
	$kt_matkhau = new xl_nguoidung();
	$kq1= $kt_matkhau->kt_matkhau($matkhau);
	
	$kt = new xl_nguoidung();
	$kq2= $kt->kt_email($email);
	
	if($hoten=="")
	{
		echo "<script>alert('Vui lòng nhập họ tên')</script>";
		$flag=0;
	}
	else if($tendangnhap=="")
	{
		echo "<script>alert('Vui lòng nhập ten đăng nhập')</script>";
		$flag=0;
	}
	else if($matkhau=="")
	{
		echo "<script>alert('Vui lòng nhập mật khẩu')</script>";
		$flag=0;
	}
	else if($email=="")
	{
		echo "<script>alert('Vui lòng nhập email')</script>";
		$flag=0;
	}
	else if(isset($kq1['manguoidung']))
	{
		echo "<script>alert('Vui lòng chọn mat khau khac')</script>";
		$flag=0;
	}
	else if(isset($kq['manguoidung']))
	{
		echo "<script>alert('Vui lòng chọn tên đăng nhập khác')</script>";
		$flag=0;
	}
	else if(isset($kq2['manguoidung']))
	{
		echo "<script>alert('Vui lòng chọn email khác')</script>";
		$flag=0;
	}
	else if(kt_kytu($hoten,$tendangnhap,$matkhau)==0)
	{
	$e = kt_kytu($hoten,$tendangnhap,$matkhau);
		echo "<script>alert('Vui lòng không nhập ký tự đặc biệt ngoài số và chữ')</script>";
		$flag=0;
	}
	else if (check_email($email)== false){
                  echo "<script>alert('Mail không đúng định dạng')</script>";
		$flag=0;
	}
	
	if($flag==1)
	{
		echo $e;
	$diachi = $_POST['txt_diachi'];
	$dienthoai = $_POST['txt_dienthoai'];
	//$hinh = $_POST["txt_hinh"];
	echo $diachi;
	echo $dienthoai;
	$them_user = new xl_nguoidung();
	$now = getdate();
	$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
	$ketqua = $them_user->them($matkhau,$hoten,$tendangnhap,$diachi,$dienthoai,$currentDate,$email);
	echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=user"  />';
	}
}

function kt_kytu($hoten,$tendangnhap,$matkhau)
{
	$kq=1;
	$specialChar="~`!@#$%^&*()-+=|\{}[]:;><,.?/".'"'; 
	for ($i=0; $i<strlen($specialChar); $i++) 
	{ 
		if (strpos($hoten, $specialChar[$i]) !==false || strpos($tendangnhap, $specialChar[$i]) !==false || strpos($matkhau, $specialChar[$i]) !==false) 
		{ 
			$kq=0;	
		} 
	} 
	return $kq;
}

function check_email($email) 
{  // hàm kiểm tra email
	$to_work_out = explode("@", $email); 
  if (!isset($to_work_out[0])) return FALSE; 
  if (!isset($to_work_out[1])) return FALSE; 

  $pattern_local = '^([0-9a-z]*([-|_]?[0-9a-z]+)*)(([-|_]?)\.([-|_]?)[0-9a-z]*([-|_]?[0-9a-z]+)+)*([-|_]?)$'; 
  $pattern_domain = '^([0-9a-z]+([-]?[0-9a-z]+)*)(([-]?)\.([-]?)[0-9a-z]*([-]?[0-9a-z]+)+)*\.[a-z]{2,4}$'; 
  $match_local = eregi($pattern_local, $to_work_out[0]); 
  $match_domain = eregi($pattern_domain, $to_work_out[1]); 

  if ($match_local && $match_domain) { 
    return true; 
  } 
  return false; 
}
		
?>